package cn.wolfcode.trip.app.controller;
import cn.wolfcode.trip.app.util.UserContext;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import cn.wolfcode.trip.base.util.JSONResult;
import jdk.nashorn.internal.objects.annotations.Getter;

import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import cn.wolfcode.trip.base.util.JSONResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("travelLikes")
public class TravelLikeController {
    @Autowired
    private ITravelLikeService travelLikeService;

    /**
     * 游记的点赞操作
     * @param travelId
     * @return
     */
    @GetMapping("{travelId}")
    public Object selectByTravelIdAndUserId(@PathVariable Long travelId){
        JSONResult result = new JSONResult();
        //进行数据库的查询,判断当前用户是否登录已经点赞
        User currentUser = UserContext.getCurrentUser();
        //获取到用户id
        Long userId = currentUser.getId();
        //通过用户与游记id进行查询
        TravelLike travelLike = travelLikeService.toByTravelIdAndUserId(travelId, currentUser);
        result.setData(travelLike.getState());
        return  result;
    }

    @GetMapping("{travelId}/likecounts")
    public Object selectBytravelId(@PathVariable Long travelId){
        JSONResult result = new JSONResult();
        //进行数据库的查询,判断当前用户是否登录已经点赞
        User currentUser = UserContext.getCurrentUser();
        //获取到用户id
        Long userId = currentUser.getId();
        //通过用户与游记id进行查询
        TravelLike travelLike = travelLikeService.selectBytravelId(travelId, currentUser);
        result.setData(travelLike.getState());
        return  result;
    }

    @GetMapping("/interest")
    public Object selectAll(int type, Long targetId){
        return travelLikeService.selectAll(type, targetId);
    }

    @PutMapping
    public void changeType(int type,Long targetId){
        travelLikeService.changeType(type, targetId);
    }
}
